/*
 * Copyright (c) 2012 Aleksander Zotov, Benedikt Markt, Aliaksei Babkovich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 *  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 *  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


package utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * This class contains mehtods to print debug log mesages
 * 
 * @author Benedikt Markt
 *
 */
public class Debug {
	public final static DateFormat df = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public final static int TRACE = 0;
	public final static int DEBUG = 1;
	public final static int INFO = 2;
	public final static int WARN = 3;
	public final static int ERROR = 4;
	public final static int FATAL = 5;

	/**
	 * Prints error and info messages to stdout
	 * 
	 * @param level the current log level, defines wich messages are printed
	 * @param message the log message
	 */
	public static void log(int level, String message) {
		ApplicationPreferences prefs = ApplicationPreferences.getInstance();
		if (level >= prefs.getLogLevel()) {
			if (level == 0) {
				System.out.print(df.format(new Date()) + " [TRACE] ");
			} else if (level == 1) {
				System.out.print(df.format(new Date()) + " [DEBUG] ");
			} else if (level == 2) {
				System.out.print(df.format(new Date()) + " [INFO]  ");
			} else if (level == 3) {
				System.out.print(df.format(new Date()) + " [WARN]  ");
			} else if (level == 4) {
				System.out.print(df.format(new Date()) + " [ERROR] ");
			} else if (level == 5) {
				System.out.print(df.format(new Date()) + " [FATAL] ");
			} else {
				return;
			}

			System.out.println(message);
		}
	}
	
	/**
	 * Prints error and info messages to stdout
	 * 
	 * @param level
	 * @param message 
	 * @param overrideLevel if true, the log message is printed even if log level is higher
	 */
	public static void log(int level, String message, Boolean overrideLevel) {
		if (overrideLevel) {
			if (level == 0) {
				System.out.print(df.format(new Date()) + " [TRACE] ");
			} else if (level == 1) {
				System.out.print(df.format(new Date()) + " [DEBUG] ");
			} else if (level == 2) {
				System.out.print(df.format(new Date()) + " [INFO]  ");
			} else if (level == 3) {
				System.out.print(df.format(new Date()) + " [WARN]  ");
			} else if (level == 4) {
				System.out.print(df.format(new Date()) + " [ERROR] ");
			} else if (level == 5) {
				System.out.print(df.format(new Date()) + " [FATAL] ");
			} else {
				return;
			}

			System.out.println(message);
		}
	}

}
